//Given two integers representing the numerator and denominator of a fraction, r
//eturn the fraction in string format. 
//
// If the fractional part is repeating, enclose the repeating part in `parenthese`
//s. 
//
// If multiple answers are possible, return any of them. 
//
// It is guaranteed that the length of the answer string is less than 104 for al
//l the given inputs. 
//
// 
// Example 1: 
// Input: numerator = 1, denominator = 2
//Output: "0.5"
// Example 2: 
// Input: numerator = 2, denominator = 1
//Output: "2"
// Example 3: 
// Input: numerator = 2, denominator = 3
//Output: "0.(6)"
// Example 4: 
// Input: numerator = 4, denominator = 333
//Output: "0.(012)"
// Example 5: 
// Input: numerator = 1, denominator = 5
//Output: "0.2"
// 
// 
// Constraints: 
//
// 
// -231 <= numerator, denominator <= 231 - 1 
// denominator != 0 
// 
// Related Topics 哈希表 数学 字符串 
// 👍 238 👎 0

package leetcode.editor.cn;

import org.junit.Assert;

import java.util.HashMap;

class P166FractionToRecurringDecimal {
    public static void main(String[] args) {
        Solution solution = new P166FractionToRecurringDecimal().new Solution();
        System.out.println(solution.fractionToDecimal(1, -2147483648));
        System.out.println("-0.0000000004656612873077392578125");
        Assert.assertEquals(solution.fractionToDecimal(1, -2147483648),
                "0.0000000004656612873077392578125");
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public String fractionToDecimal(int x, int y) {
            HashMap<Long, Integer> map = new HashMap<>();
            long a = x, b = y;
            if (a == 0) {
                return "0";
            }
            int sys;
            if (a > 0 && b > 0 || a < 0 && b < 0) {
                sys = 1;
            } else {
                sys = -1;
            }
            StringBuilder sb = new StringBuilder();
            boolean hasdec = false;
            while (a != 0) {
                while (a / b == 0) {
                    if (!hasdec) {
                        sb.append(".");
                        hasdec = true;
                    } else {
                        sb.append("0");
                    }
                    a = a * 10;
                }
                if (map.containsKey(a) && sb.lastIndexOf(".") != sb.length() - 1) {
                    int start = map.get(a);
                    sb.insert(start, "(");
                    sb.append(")");
                    break;
                } else {
                    map.put(a, sb.length());
                    sb.append(Math.abs(a / b));
                    a = a % b;
                }
                if (hasdec) {
                    a = a * 10;
                }
            }
            if (sb.lastIndexOf(".") == 0) {
                sb.insert(0, "0");
            }
            return sys > 0 ? sb.toString() : "-" + sb.toString();
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}